package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyBaiQianZiXun;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeBaiQianZiXun;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 【机构】【下游】安心-佰干咨询API苏州五选40
 * @ Author 江小游
 * @ Date 2025/4/24 10:07
 */
@ApiPlan(productId = "161", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5BaiQianZiXun implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        OrgDataExchangeBaiQianZiXun orgDataExchangeBaiQianZiXun=new OrgDataExchangeBaiQianZiXun();
        orgDataExchangeBaiQianZiXun.setTel(apiDataExchangeParameter.getPhoneMd5());
        return JsonUtils.toJson(orgDataExchangeBaiQianZiXun);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        //实体类
        OrgApplyBaiQianZiXun orgApplyBaiQianZiXun=new OrgApplyBaiQianZiXun();
        //拓展
        ApiFieldConverter apiFieldConverter=ApiFieldConverter.create(apiApplyParameter);
        //姓名	string	张三
        orgApplyBaiQianZiXun.setCname(apiApplyParameter.getUserName());
        //手机号	string	13800138000
        orgApplyBaiQianZiXun.setTel(apiApplyParameter.getPhone());
        //年龄	int	18
        orgApplyBaiQianZiXun.setAge(apiFieldConverter.getAgeInt());
        //性别	int	0男/1女
        Integer sex= ChooseEq.create()
                .when(apiApplyParameter.getSex(),"0",0)
                .when(apiApplyParameter.getSex(),"1",1)
                .end(0);
        orgApplyBaiQianZiXun.setSex(sex);

        //职业	string	程序员
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            String job= ChooseEq.create()
                    .when(apiApplyParameter.getCareer(),"1","上班族")
                    .when(apiApplyParameter.getCareer(),"2","自由职业")
                    .when(apiApplyParameter.getCareer(),"3","企业主")
                    .when(apiApplyParameter.getCareer(),"4","公务员")
                    .when(apiApplyParameter.getCareer(),"5","个体户")
                    .end("自由职业");
            orgApplyBaiQianZiXun.setJob(job);
        }
        //贷款额度	string	8（单位：万）
        orgApplyBaiQianZiXun.setEd(apiFieldConverter.getLoanAmountUnit());
        //房产	int	0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                    .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                    .end(0);
            orgApplyBaiQianZiXun.setFc(house);
        }
        //车产	int	0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            Integer car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", 0)
                    .when(apiApplyParameter.getCarInfo(), "2", 1)
                    .when(apiApplyParameter.getCarInfo(), "3", 1)
                    .when(apiApplyParameter.getCarInfo(), "4", 1)
                    .end(0);
            orgApplyBaiQianZiXun.setCc(car);
        }
        //公积金	int	0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            Integer value = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", 0)
                    .when(apiApplyParameter.getProvidentFund(), "2", 1)
                    .when(apiApplyParameter.getProvidentFund(), "3", 1)
                    .when(apiApplyParameter.getProvidentFund(), "4", 1)
                    .end(0);
            orgApplyBaiQianZiXun.setGjj(value);
        }
        //保单	int	0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            Integer value = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                    .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                    .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                    .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                    .end(0);
            orgApplyBaiQianZiXun.setBd(value);
        }
        //社保	int	0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            Integer value = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                    .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                    .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                    .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                    .end(0);
            orgApplyBaiQianZiXun.setSb(value);
        }
        //房产说明	string
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String houseExplain = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "无房")
                    .when(apiApplyParameter.getRealEstateInfo(), "2", "有房不抵押")
                    .when(apiApplyParameter.getRealEstateInfo(), "3", "有房可抵娅")
                    .when(apiApplyParameter.getRealEstateInfo(), "4", "有房")
                    .when(apiApplyParameter.getRealEstateInfo(), "5", "有按揭房")
                    .when(apiApplyParameter.getRealEstateInfo(), "6", "有全款房")
                    .end("无房");
            orgApplyBaiQianZiXun.setFcsm(houseExplain);
        }
        //车产说明	string
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String carExplain = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无车")
                    .when(apiApplyParameter.getCarInfo(), "2", "有车不抵押")
                    .when(apiApplyParameter.getCarInfo(), "3", "有车可抵押")
                    .when(apiApplyParameter.getCarInfo(), "4", "有车")
                    .end("无车");
            orgApplyBaiQianZiXun.setCcsm(carExplain);
        }
        //社保说明	string
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String sbExplain = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "无社保")
                    .when(apiApplyParameter.getSocialSecurity(), "2", "缴纳6个月以下")
                    .when(apiApplyParameter.getSocialSecurity(), "3", "缴纳6个月以上")
                    .when(apiApplyParameter.getSocialSecurity(), "4", "有社保")
                    .end("无社保");
            orgApplyBaiQianZiXun.setSbsm(sbExplain);
        }
        //保单说明	string
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String bdExplain = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "无保单")
                    .when(apiApplyParameter.getInsurancePolicy(), "2", "缴纳未满一年")
                    .when(apiApplyParameter.getInsurancePolicy(), "3", "缴纳一年以上")
                    .when(apiApplyParameter.getInsurancePolicy(), "4", "有保险保单")
                    .end("无保单");
            orgApplyBaiQianZiXun.setBdsm(bdExplain);
        }
        //公积金说明	string
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String carExplain = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "无公积金")
                    .when(apiApplyParameter.getProvidentFund(), "2", "缴纳6个月以下")
                    .when(apiApplyParameter.getProvidentFund(), "3", "缴纳6个月以上")
                    .when(apiApplyParameter.getProvidentFund(), "4", "有公积金")
                    .end("无公积金");
            orgApplyBaiQianZiXun.setGjjsm(carExplain);
        }
        //收入	string	数字字符串均可
        if (ValidParam.isNotEmpty(apiApplyParameter.getMonthlySalary())) {
            String salary = ChooseEq.create()
                    .when(apiApplyParameter.getMonthlySalary(), "1", "3000")
                    .when(apiApplyParameter.getMonthlySalary(), "2", "8000")
                    .when(apiApplyParameter.getMonthlySalary(), "3", "15000")
                    .when(apiApplyParameter.getMonthlySalary(), "4", "30000")
                    .when(apiApplyParameter.getMonthlySalary(), "5", "50000")
                    .end("3000");
            orgApplyBaiQianZiXun.setSr(salary);
        }
        //芝麻分	string	数字字符串均可
        if (ValidParam.isNotEmpty(apiApplyParameter.getSesameCredit())) {
            String zhima = ChooseEq.create()
                    .when(apiApplyParameter.getSesameCredit(), "1", "600分以下")
                    .when(apiApplyParameter.getSesameCredit(), "2", "600~649分")
                    .when(apiApplyParameter.getSesameCredit(), "3", "650~699分")
                    .when(apiApplyParameter.getSesameCredit(), "4", "700分以上")
                    .end("600分以下");
            orgApplyBaiQianZiXun.setZmf(zhima);
        }
        //城市	string	上海
        orgApplyBaiQianZiXun.setCity(apiApplyParameter.getCity().replace("市", ""));
        //备注	string	其他资质说明
        orgApplyBaiQianZiXun.setRemarks(apiApplyParameter.getFormRemark());
        return JsonUtils.toJson(orgApplyBaiQianZiXun);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
